<template>
  <div class="purchase-order">
    <div class="search-condition">
      <el-form label-width="120px">
        <el-form-item label="客户编号">
          <el-input v-model="input" placeholder="请输入内容"></el-input>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="search">查询</el-button>
        </el-form-item>
      </el-form>
    </div>
    <div>
      <addCustomer />
    </div>
    <el-table :data="tableData" style="width: 100%" border stripe :header-cell-style="{ background: '#fafafa' }">
      <el-table-column align="center" label="序号">
        <template slot-scope="scope">
          {{ scope.$index + 1 + (data.pageNum - 1) * data.pageSize }}
        </template>
      </el-table-column>
      <el-table-column prop="cusCode" label="客户编号" align="center"></el-table-column>
      <el-table-column prop="cusName" label="客户名称" align="center"></el-table-column>
      <el-table-column prop="cusPhone" label="客户联系方式" align="center"></el-table-column>
      <el-table-column prop="createName" label="创建人" align="center"></el-table-column>
      <el-table-column prop="createStr" label="创建时间" align="center"></el-table-column>
      <el-table-column prop="modifyName" label="最后修改人" align="center"></el-table-column>
      <el-table-column prop="modifyStr" label="最后修改时间" align="center"></el-table-column>
      <el-table-column label="操作" align="center">
        <template slot-scope="scope">
          <el-button size="mini" @click="getCusById(scope.row.id)">编辑</el-button>
          <el-button size="mini" type="danger" @click="delCus(scope.row.id)">删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    <div class="block">
      <el-pagination background layout="total,prev, pager, next, jumper" :total="data.total" :page-size="data.pageSize"
        :current-page="data.currentPage" @current-change="handleCurrentChange"></el-pagination>
    </div>
    <el-drawer title="客户修改" :before-close="handleClose" :visible.sync="addVisible" direction="rtl"
      custom-class="demo-drawer" ref="drawer">
      <div class="demo-drawer__content">
        <el-form>
          <el-form-item label="姓名: " prop="cusName" :label-width="formLabelWidth">
            <el-input v-model="cusName" autocomplete="off"></el-input>
          </el-form-item>
          <el-form-item label="联系方式: " prop="cusPhone" :label-width="formLabelWidth">
            <el-input v-model="cusPhone" autocomplete="off"></el-input>
          </el-form-item>
        </el-form>
        <div class="demo-drawer__footer" align="center">
          <el-button @click="addVisible = false">取 消</el-button>
          <el-button type="primary" @click="updCus()">修改</el-button>
        </div>
      </div>
    </el-drawer>
  </div>
</template>

<script>
import addCustomer from "./AddCustomer.vue"
export default {
  components: { addCustomer },
  name: 'customerManage',
  data() {
    return {
      data: {
      },
      customer:{},
      addVisible: false,
      cusName: '',
      cusPhone: '',
      formLabelWidth: '80px',
      pageSize: 5,
      tableData: [],
      input: ''
    };
  },
  methods: {
    handleClose() {
      this.addVisible = false;
    },
    page(pageNo, pageSize) {
      this.$http.post("http://localhost:8081/finerCustomer/getCustomerList", {
        pageNo: pageNo,
        pageSize: pageSize,
        cusName: this.input
      })
        .then((res) => {
          this.data = res.data.data;
          this.tableData = res.data.data.list;
        })
    },
    search() {
      this.page(1, this.data.pageSize);
    },
    handleCurrentChange(currentPage) {
      this.data.currentPage = currentPage;
      this.page(currentPage, this.data.pageSize);
    },
    updCus(id) {
      var phoneReg = /(^1\d{10}$)|(^[0-9]\d{7}$)/;
      if (this.cusName === '') {
        alert("姓名不能为空!");
        return;
      }
      if (this.cusPhone === '') {
        alert("联系方式不能为空!");
        return;
      }
      if (!phoneReg.test(this.cusPhone)) {
        alert("手机号码格式错误!");
        return;
      }
      this.$http.post("http://localhost:8081/finerCustomer/updCus", {
        id: this.customer.id,
        userId: 1,
        cusName: this.cusName,
        cusPhone: this.cusPhone,
        cusCode:this.customer.cusCode
      })
        .then((res) => {
          if (res.data.code === '200') {
            alert(res.data.message);
            this.handleClose();
          }
        })
    },
    getCusById(id) {
      this.addVisible = true;
      this.$http.post("http://localhost:8081/finerCustomer/getCusById", {
        id: id
      })
        .then((res) => {
          this.cusName = res.data.data.cusName;
          this.cusPhone = res.data.data.cusPhone;
          this.customer = res.data.data;
          this.customer.id = id;
          
        })
    },
    delCus(id) {
      if (confirm("确定删除该客户吗？")) {
        this.$http.post("http://localhost:8081/finerCustomer/delCus", {
          id: id
        })
          .then((res) => {
            if (res.data.code === '200') {
              alert(res.data.message);
              this.page(1, this.pageSize);
            }
          })
      }
    }
  },
  mounted() {
    this.page(1, this.pageSize);
  }
}
</script>

<style>
.cusCode {
  color: #0090ff !important;
  cursor: pointer;
}

.block {
  text-align: right;
  margin-top: 20px;
}

.total-count {
  margin-bottom: 10px;
  font-weight: bold;
}
</style>